<?php
namespace App\Http\Controllers;
use App\Finish;
use App\Good;
use App\Goodssecond;
use App\Ordertask;
use App\Task;
use App\Work;
use App\Order;
use App\Workshop;
use App\Worker;
use Carbon\Carbon;
use Illuminate\Http\Request;
class FlowController extends Controller
{
    public function index()
    {
        return view('flow.flow');
    }

    public function end()
    {
        return view('flow.flowend');
    }

    public function home()
    {
        return view('flow.home');
    }
    public function makeSure($code)
    {
        $work = Work::find($code);
        $work->can = 1;
        $work->save();
        // dd($task);
        return response()->json($work);


    }
    public function notSure($code)
    {

        $work = Work::find($code);
        $work->can=0;
        $work->save();
        // dd($task);
        return response()->json($work);

    }


    public function getTask($code)
    {
        $date=Carbon::now();
        $yet=Carbon::now()->subDays(3);
        $yet=date('Y-m-d',strtotime($yet));
        $today =date('Y-m-d',strtotime($date));
        $worker=Worker::where('workerCode',$code)->first();
        if(!$worker){
          return response()->json('0');
            dd();
        }
        $workerId=$worker['id'];
        $task = Task::where('workerId', $workerId)->whereBetween('date',[$yet,$today])->get();

        if(!$task){
            return response()->json('0');
        }else{
            $tasks=array();
            $i=0;
            foreach ($task as $t){
                $tasks[]=$t;
                $work=Work::find($t->workId);
                $tasks[$i]['machine']=$work->machineName;
                $tasks[$i]['workshop']=$work->machineWorkshopName;
                $i=$i+1;
            }
        }
            // dd($task);
        return response()->json($tasks);

    }
    public function getNext($code)
    {
        $date=Carbon::tomorrow();
        $next =date('Y-m-d',strtotime($date) );
        $worker=Worker::where('workerCode',$code)->first();
        if(!$worker){
            return response()->json('0');
            dd();
        }
        $workerId=$worker['id'];
        $work = Work::where('workerId', $workerId)->where('date',$next)->get();
        if(!$work){
            return response()->json('0');
        }
        // dd($task);
        return response()->json($work);

    }
    public function getTaskWorkshop($code)
    {
        $date=Carbon::now();
        $today =date('Y-m-d',strtotime($date) );
        $worker=Worker::where('workerCode',$code)->first();
        if(!$worker){
            return response()->json('0');
            dd();
        }
        $flowEndCode=$worker['id'];
        $todaygoodsTask = Task::where('workerId', $flowEndCode)->where('date',$today)->get();
        if(!$todaygoodsTask){
            return response()->json('0');
            dd();
        }
        // dd($todaygoodsTask);
        return response()->json($todaygoodsTask);

    }

    public function getTaskOutput($id)
    {

       $task=Task::find($id);
        $work=Work::find($task->workId);
        $workshop=Workshop::find($work->machineWorkshopId);

        $output=$task->output*$workshop->weigh;
        // dd($todaygoodsTask);
        return response()->json($output);

    }


    public function workerCode(Request $request)
    {
        $date = Carbon::now();
        $tomorrow = Carbon::tomorrow();
        $today = date('Y-m-d', strtotime($date));
        $tomorrow = date('Y-m-d', strtotime($tomorrow));
        $workerCode = $request->input('workerCode');
        $worker=Worker::where('workerCode',$workerCode)->first();
        if($worker){
            $workerid=$worker->id;
            $todayWorkTasks = Work::where('workerId', $workerid)->where('date', $today)->first();
            if ($todayWorkTasks) {
                $workId = $todayWorkTasks->id;
                $todayTask = Task::where('workId', $workId)->first();
                if($todayTask){
                    $goodsId = $todayTask->goodsId;
                    $todayParts = Good::find($goodsId)->children()->get();
                }else{
                    $todayTask=Null;
                    $todayParts=Null;
                }
                $workshop['today']=$todayWorkTasks->machineWorkshopName.'_'.$todayWorkTasks->machineName;
                if($todayWorkTasks->workType ==1){
                    $workshop['todayType']='白班';
                }else{
                    $workshop['todayType']='晚班';
                    }
            }else{
                $todayTask=Null;
                $todayParts=Null;
                $workshop['today']='无信息';
            }
            $tomorrowWorkTasks = Work::where('workerId', $workerid)->where('date', $tomorrow)->first();
            if ($tomorrowWorkTasks) {
                $tworkid=$tomorrowWorkTasks->id;
                $workcan=$tomorrowWorkTasks->can;
                $tomorrowTask = Task::where('workId',$tworkid)->first();
                if($tomorrowTask){
                $goodsId = $tomorrowTask->goodsId;
                $tomorrowParts = Good::find($goodsId)->children()->get();
                }else{
                    $tomorrowTask=Null;
                    $tomorrowParts=Null;
                }
                $workshop['tomorrow']=$tomorrowWorkTasks->machineWorkshopName.'_'.$tomorrowWorkTasks->machineName;
                if($tomorrowWorkTasks->workType ==1){
                    $workshop['tomorrowType']='白班';
                }else{
                    $workshop['tomorrowType']='晚班';
                }
            }else{
                $tomorrowTask=Null;
                $tomorrowParts=Null;
                $tworkid='0';
                $workcan='0';
                $workshop['tomorrow']='无信息';
            }
            return view('flow.flow',['todayTasks'=>$todayTask,'tomorrowTasks'=>$tomorrowTask,'todayParts'=>$todayParts,'tomorrowParts'=>$tomorrowParts,'tworkId'=>$tworkid,'worker'=>$worker,'workshop'=>$workshop,'workcan'=>$workcan]);

        }else{
            return view('flow.flow');
        }

    }

    public function flowendcode(Request $request)
    {
        $date=Carbon::now();
        $today =date('Y-m-d',strtotime($date) );
        $flowEndCode=$request->input('flowEndCode');
        $worker=Worker::where('workerCode',$flowEndCode)->first();
        $flowEndCode=$worker['id'];
        $todayIsAllot = Work::where('workerId', $flowEndCode)->where('date', $today)->count();
        if ($todayIsAllot == 1) {
            $todayWorkTasks = Work::where('workerId', $flowEndCode)->where('date', $today)->get();
            foreach ($todayWorkTasks as $todayWorkTasks) {
                $workId = $todayWorkTasks->id;
                $todaygoodsTask = Task::where('workId',$workId)->first();
               // dd($todaygoodsTask);
                return view('flow.flowend',['todayWorkTasks'=>$todaygoodsTask]);
            }
        }else{
            return view('flow.flowend');
        }

    }

//    public function flowFinish(Request $request)
//    {
//
//        $date=Carbon::now();
//        $finishOne ['date'] =date('Y-m-d',strtotime($date) );
//        $finish=$request->input('finish');
//        $finishOne ['taskId'] =$taskId=$request->input('WorkTasksId');
//        $todaygoodsTask = Task::find($taskId);
//        $goods=Good::find($todaygoodsTask->goodsId);
//        $weigh=$goods->weight;
//        $finishOne ['finish'] =$finish / $weigh/10000;
//        $finishOne ['log'] =0;
//        Finish::Create($finishOne);
//        $finishAll=Finish::where('taskId',$taskId)->where('log',0)->get();
//       // dd($finishAll);
//        return view('flow.flowend',['todayWorkTasks'=>$todaygoodsTask,'finish'=>$finishAll]);
//    }

    public function finished(Request $request)
    {
        $taskInfo['taskId'] = $taskId = $request->input('taskId');
        $taskInfo['finish'] =  $finish=$request->input('finish');
        $task = Task::find($taskId);
        $worker = Worker::find($task->workerId);
        /************            交接记录            ***********/
        $date = Carbon::now();
        $taskInfo['date'] = date('Y-m-d', strtotime($date));
        $taskInfo['workId'] = $task->workId;
        $taskInfo['goodsId'] =$goodId=$task->goodsId;
        $taskInfo['goodsName'] = $task->goodsName;
        $taskInfo['workerId'] = $task->workerId;
        $taskInfo['workerName'] = $worker->workerName;
        $taskInfo['workerCode'] = $worker->workerCode;
        $taskInfo['orderTaskId'] = $task->orderTaskId;
        /************            交接记录            ***********/

        $work = Work::find($task->workId);
        $workshop = Workshop::find($work->machineWorkshopId);
        $weigh = $workshop->weigh;
        $goods = Good::find($task->goodsId);
        $goodsWeight = $goods->weight;

//        $inputF = $request->input('finish') * $weigh/$goodsWeight/10000;
//
//        if ($request->input('finish') === 0 and $request->input('finish') === '' ) {
//           dd();
//        }
//        if ($request->input('otherFinish') === 0 and $request->input('otherFinish' == '')) {
//            $inputOF = 0;
//        } else {
//            $inputOF = $request->input('otherFinish') / 10000;
//        }
//        $taskInfo['finish'] = $finish = $inputF + $inputOF;
//

        $orderTask = Ordertask::find($task->orderTaskId);
        $taskInfo['orderId'] = $orderTask->orderId;

        $order=Order::find($orderTask->orderId);
        $taskInfo['orderNo']=$order->orderNo;

        Finish::Create($taskInfo);  //添加交接记录
        $task->finish =$task->finishs()->sum('finish');
        $task->save();


        $orderTask->finished = $childOrdertaskFinish =$orderTask->tasks()->sum('finish');
       // $orderTask->realFinish = $orderTask->tasks()->sum('finish');
        $orderTask->save();

//        $finishSum=Ordertask::where('orderId',$orderTask->orderId)->sum('finished');
//        echo $finishSum;
//        $outputSum=Ordertask::where('orderId',$orderTask->orderId)->sum('output');
//        echo $outputSum;
//        $bfb=$finishSum/$outputSum*100;

        $order->finished=$orderfinish=$order->ordertasks()->sum('finished');
        $order->save();

        $childOrder=$order->children()->get();   //获取合并订单下的子订单信息并更新
        if($childOrder){
            foreach ($childOrder as $cO){
                Order::where('id',$cO->id)->update(['finished'=>$orderfinish]);
                Ordertask::where('orderId',$cO->id)->where('goodsId',$goodId)->update(['finished'=>$childOrdertaskFinish]);
            }
        }


        return response()->json('信息提交成功');

    }

    public function finishc(Request $request)
    {
        $taskId=$request->input('taskId');
        Finish::where('taskId',$taskId)->delete();
        return view('flow.flowend');
    }
    public function pass(Request $request)
    {
        $workshopId=$request->input('workshopId');
        $pass=$request->input('password');

        $count=Workshop::where('id',$workshopId)->where('pass',$pass)->count();
        return response()->json($count);
    }
    public function canWork(Request $request)
    {
       echo  $id=$request->input('workId');
        $work=Work::find($id);
        $work->can=1;
        $work->save();
        return view('flow.flow');
    }

    public function notWork(Request $request)
    {
       echo  $id=$request->input('workId');
        $work=Work::find($id);
        $work->can=0;
        $work->save();
        return view('flow.flow');
    }

}